#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

clear;

# VAR   ******************************************************************
vVersion='1.6.2_v2020528';
vPlugins='/app/chevereto';
vl10n='/app/chevereto/app/lib';
vlang='/app/chevereto/app/content/languages';
vgetText='/app/chevereto/lib/G/classes';
vAction=$1;
vWorker=$2
pack='https://gitee.com/yangjm123/chevereto_chinese.git'
dist='chevereto_chinese'
# Logo  ******************************************************************
CopyrightLogo="
                         Chevereto Chinese Package $vVersion
                                    by Yangjm
==========================================================================";
echo "$CopyrightLogo";

# Function List *******************************************************************************
function install()
{
    cd /tmp/;

    # backup
    if [ ! -f "$vl10n/l10n.php.orig" ]; then
        mv $vl10n/l10n.php $vl10n/l10n.php.orig
    fi;
    if [ ! -f "$vlang/zh-CN.po.orig" ]; then
        mv $vlang/zh-CN.po $vlang/zh-CN.po.orig
    fi;
    if [ ! -f "$vPlugins/app/loader.php.orig" ]; then
        mv $vPlugins/app/loader.php $vPlugins/app/loader.php.orig
    fi;
    if [ ! -f "$vgetText/class.gettext.orig" ]; then
        mv $vgetText/class.gettext $vgetText/class.gettext.orig
    fi;

    wget --no-check-certificate $pack -O chevereto_chinese.tar.gz;
    tar -zxvf chevereto_chinese.tar.gz

        cd $dist

    \cp -rfa ./app $vPlugins/;
    \cp -rfa ./lib $vPlugins/;
    
    chmod 0755 $vPlugins/app/content/languages/zh-CN.po $vPlugins/app/lib/l10n.php $vPlugins/app/loader.php

    cd -

    echo '==========================================================================';
    echo "Congratulations, Chevereto Chinese Package $vVersion install/upgrade completed.";
    echo '==========================================================================';
}

function uninstall()
{
    mv -f $vl10n/l10n.php.orig $vl10n/l10n.php
    mv -f $vPlugins/app/loader.php.orig $vPlugins/app/loader.php
    mv -f $vgetText/class.gettext.orig $vgetText/class.gettext

    rm $vlang/zh-CN.po

    rm -rf /tmp/$dist;

    echo 'Congratulations, Chevereto Chinese Package uninstall completed.';
    echo '==========================================================================';
}

# SHELL     ******************************************************************
if [ "$vAction" == 'install' ]; then
    if [ ! -f "$vl10n/l10n.php.orig" ]; then
        install;
    else
        echo 'Sorry, you have already installed Chevereto Chinese Package.';
        echo '==========================================================================';
        exit 1;
    fi;
elif [ "$vAction" == 'uninstall' ]; then
    if [ ! -f "$vl10n/l10n.php.orig" ]; then
        echo 'Sorry, you have not installed Chevereto Chinese Package yet.';
        echo '==========================================================================';
        exit 1;
    else
        uninstall;
    fi;
elif [ "$vAction" == 'upgrade' ]; then
    install;
else
    echo 'Sorry, Failed to install DS Video Douban Patch.';
    echo '==========================================================================';
    exit 1
fi;